//
//  LearningTVController.swift
//  ddx0513
//
//  Created by shuijing on 15/7/9.
//  Copyright (c) 2015年 jiang yongbin. All rights reserved.
//

import UIKit

class LearningTVController: UITableViewController {
    
    var lessonnameArr:NSMutableArray = []
    var totalSectionNum = 0
    var chapterName:NSMutableArray = []
    var sectionName:NSMutableArray = []
    var sectionId:NSMutableArray = []
    var sectionImg:NSMutableArray = []
    var chatperId = 0
    var sectionSubnum = 0
   // var isChapter:Bool = false
    var isChapter:NSMutableArray = []
    var studied:NSMutableArray = []
    var progress:NSMutableArray = []
    var isStudiedChapter:NSMutableArray = []
    var kindOfImage:Int = 0
   // var progressImg:NSMutableArray = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        getLearningCourse()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }
    
    func getLearningCourse(){
        
        var userid:Int = 1
        var token:String = DBUtils.mem.token
        var lessonId:Int = 1
        NetUtils.netRequest(Method.GET, URLString: NetUtils.getURLLessonInfo() , parameters: ["userid":userid,"token":token,"lessonid":lessonId], responseHandler: NetUtils.doNothing, netErrorHandler: NetUtils.doNothing, successHandler: learningTableview, interErrorHandler: NetUtils.doNothing, jsonErrorHandler: NetUtils.doNothing)
        
        
        
    }
    func learningTableview (json: JSON) {
        NSLog("json int detail")
        println(json)
        totalSectionNum = json["sectionnum"].int!
        chatperId = json["chapters"][0]["chatperid"].int!
        println(chatperId)
        chapterName.addObject(json["chapters"][2]["chaptername"].string!)
        println("list begining")
        var k = 0
        var lastSection:Bool = false
        var isStudied:Bool = false
        var isProgressed = 0
        //var isChapterStudied:Bool = false
        //var isChapterProgressed = 0

        
        for (var i = chatperId-1;i >= 0; i-- )
        {
  
            lessonnameArr.addObject(json["chapters"][i]["chaptername"].string!)
            isChapter.addObject(true)
            sectionSubnum = json["chapters"][i]["sectionsubnum"].int!
            println(json["chapters"][i]["chaptername"].string!)
            //isChapterStudied = json["chapters"][i]["sections"][0]["studied"].bool!
            //isChapterProgressed = json["chapters"][i]["sections"][0]["progress"].int!

            for (var j = sectionSubnum-1;j >= 0; j-- )
            {
                println(json["chapters"][i]["sections"][j]["studied"].bool!)
                println("look")
                isStudied = json["chapters"][i]["sections"][j]["studied"].bool!
                isProgressed = json["chapters"][i]["sections"][j]["progress"].int!
                
                
                if (j==sectionSubnum-1)
                {
                    if( isStudied ){
                        isStudiedChapter.addObject(1)
                        isStudiedChapter.addObject(1)
                    }
                    else if(!isStudied && isProgressed != 0){
                        isStudiedChapter.addObject(1)
                        isStudiedChapter.addObject(2)
                        
                    }
                    else if(!isStudied && isProgressed == 0){
                        isStudiedChapter.addObject(3)
                        isStudiedChapter.addObject(3)
                        
                    }
     
                    
                }
                else
                {
                    if( isStudied ){
                        isStudiedChapter.addObject(1)
                    }
                    else if(!isStudied && isProgressed != 0){
                        isStudiedChapter.addObject(2)
                        
                    }
                    else if(!isStudied && isProgressed == 0){
                        isStudiedChapter.addObject(3)
                        
                    }

                    
                }
                
                studied.addObject(json["chapters"][i]["sections"][j]["studied"].bool!)

                lessonnameArr.addObject(json["chapters"][i]["sections"][j]["sectionname"].string!)
                isChapter.addObject(false)
                println(json["chapters"][i]["sections"][j]["sectionname"].string!)
                
                progress.addObject(json["chapters"][i]["sections"][j]["progress"].int!)
            }
     //       isStudiedChapter.addObject(1)
            
        }

        
         self.tableView.reloadData()
        
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return lessonnameArr.count
       
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       // UITableViewCellSeparatorStyleNone
        var tableCell :LearningItem = tableView.dequeueReusableCellWithIdentifier("learning") as! LearningItem
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        var s = lessonnameArr.objectAtIndex(indexPath.row) as? String
        var b = isChapter.objectAtIndex(indexPath.row) as! Bool
        if(b == true)
        {
            tableCell.chapterName.text = s
            tableCell.chapterName.textColor = UIColor.blackColor()
            tableCell.chapterName.font = UIFont.systemFontOfSize(14)
              //  UIFont(name: s!, size: 18.0)
        }
        else
        {
            tableCell.chapterName.text = s
            tableCell.chapterName.textColor = UIColor.grayColor()
            tableCell.chapterName.font = UIFont.systemFontOfSize(12)
        }
        var m = isStudiedChapter.objectAtIndex(indexPath.row) as! Int
        println(m)
        if (m == 1){
            tableCell.progressIMg.image = UIImage(named: "教程详情-继续学习_17@2x.png")
        }else if(m == 2){
            tableCell.progressIMg.image = UIImage(named: "教程详情-继续学习_20@2x.png")
            
        }else{
            tableCell.progressIMg.image = UIImage(named: "教程详情-开始学习_05@2x.png")
            
        }
        
        return tableCell
    }


    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

}
